package com.example.naodian.controller;


import com.example.naodian.constant.JwtClaimsConstant;
import com.example.naodian.domain.dto.*;
import com.example.naodian.domain.po.User;
import com.example.naodian.domain.vo.LoginVO;
import com.example.naodian.domain.vo.UserVO;
import com.example.naodian.properties.JwtProperties;
import com.example.naodian.result.PageResult;
import com.example.naodian.result.Result;
import com.example.naodian.service.IUserService;
import com.example.naodian.utiles.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author icewx
 * @since 2025-04-14
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户相关接口")
@AllArgsConstructor
public class UserController {
    private final IUserService userService;
    private final JwtProperties jwtProperties;

    /**
     * 用户登录接口
     * @param loginDto
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public Result<LoginVO> login(@RequestBody LoginDto loginDto){
        log.info("用户登录:{}",loginDto);
        User user = userService.login(loginDto);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ID, user.getId());
        claims.put(JwtClaimsConstant.ROLE,user.getRole());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        LoginVO loginVO = LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .token(token)
                .build();

        return Result.success(loginVO);
    }

    /**
     * 注册驾驶员账号(用户注册)
     * @param resUserDto
     * @return
     */
    @ApiOperation("注册驾驶员账号")
    @PostMapping("/reg")
    public Result reg(@RequestBody ResUserDto resUserDto){
        userService.reg(resUserDto);
        return Result.success();
    }

    /**
     * 用户分页查询
     * @param userPageQueryDto
     * @return
     */
    @ApiOperation("用户分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(UserPageQueryDto userPageQueryDto){
        PageResult pageResult = userService.pageQuery(userPageQueryDto);
        return Result.success(pageResult);
    }

    /**
     * 添加驾驶员(管理员添加)
     * @param userDto
     * @return
     */
    @ApiOperation("添加驾驶员")
    @PostMapping("/add")
    public Result add(@RequestBody UserDto userDto){
        log.info("新增驾驶员用户名:{}",userDto);
        userService.add(userDto);
        return Result.success();
    }

    /**
     * 根据id查询驾驶员信息
     * @param id
     * @return
     */
    @ApiOperation("根据id查询驾驶员信息")
    @GetMapping("/{id}")
    public Result<UserVO> getUserById(@PathVariable Long id){
        log.info("驾驶员id为:{}",id);
        UserVO userVO = userService.getUserById(id);
        return Result.success(userVO);
    }

    /**
     * 修改密码
     * @param editUserDto
     * @return
     */
    @ApiOperation("修改密码")
    @PutMapping("/editUsernameAndPassWord")
    public Result editUsernameAndPassWord(@RequestBody EditUserDto editUserDto){
        userService.editUsernameAndPassWord(editUserDto);
        return Result.success();
    }

    /**
     * 根据用户id修改用户的状态
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("根据用户id修改用户的状态")
    @PutMapping("/editRoleOrStatus")
    public Result editRoleOrStatus(Integer status,Long id){
        userService.editRoleAndStatus(status,id);
        return Result.success();
    }

    /**
     * 根据id删除驾驶员
     * @param id
     * @return
     */
    @ApiOperation("根据id删除驾驶员")
    @DeleteMapping("/{id}")
    public Result remove(@PathVariable Long id){
        log.info("删除驾驶员id为:{}",id);
        userService.removeById(id);
        return Result.success();
    }
}
